package com.omo.infrastructure.excel;

import com.aspose.cells.SaveFormat;
import com.aspose.cells.Workbook;
import com.aspose.cells.Worksheet;
import com.omo.domain.common.excel.service.IExportExcelService;
import com.omo.types.excel.BaseExportExcelData;
import com.omo.types.excel.BaseExportExcelInfo;
import com.omo.types.tools.files.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.system.ApplicationHome;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.util.Collection;

@Slf4j
public abstract class AbstractExcelExport implements IExportExcelService {
    @Override
    public void exportExcel(BaseExportExcelInfo.BaseExportExcelEntity data) {
        //1. 从数据中获取扩容参数
        Integer expandParams = getExpandParams(data);
        Collection expandParamsCollection = getExpandParamsCollection(data);
        //2. 扩充Excel表格
        byte[] newExcelTemplateBytes = processExcelTemplate(data.getTemplatePath(),data.getExpandBeginIndex(),data.getExpandBeginIndexASCii(),expandParams,expandParamsCollection);
        //3. 设置导出文件
        File outPutFile = buildOutPutFile(data.getSuffix());
        writeDataToExcel(outPutFile, data.getData(),newExcelTemplateBytes);
    }

    private Collection getExpandParamsCollection(BaseExportExcelInfo.BaseExportExcelEntity data) {
        return data.getExpandCollection();
    }


    private Integer getExpandParams(BaseExportExcelInfo.BaseExportExcelEntity data){
        return data.getExpandParam();
    }

    private byte[] processExcelTemplate(String excelTemplatePath, Integer expandBeginIndex, Integer expandBeginIndexASCii, Integer expandParams, Collection expandParamsCollection){
        try (InputStream templateInputStream = this.getClass().getClassLoader().getResourceAsStream(excelTemplatePath);
             ByteArrayOutputStream modifiedStream = new ByteArrayOutputStream()
        ){
            Workbook workbook = new Workbook(templateInputStream);
            Worksheet worksheet = workbook.getWorksheets().get(0);
            refactorTemplate(worksheet,expandBeginIndex,expandBeginIndexASCii,expandParams,expandParamsCollection);
            workbook.save(modifiedStream, SaveFormat.XLSX);
            workbook.save("test.xlsx",SaveFormat.XLSX);
            return modifiedStream.toByteArray(); // 返回修改后的内容
        }catch (Exception e){
            log.error("processExcelTemplate error:{}",e.getMessage());
        }
        return null;
    }

    protected abstract void refactorTemplate(Worksheet worksheet, Integer expandBeginIndex, Integer expandBeginIndexAscii,
                                             Integer expandParams, Collection<?> expandParamsCollection);

    protected abstract void writeDataToExcel(File outPutFile, BaseExportExcelData data, byte[] newExcelTemplateBytes);


    private File buildOutPutFile(String suffix){
        ApplicationHome applicationHome = new ApplicationHome(this.getClass());
        String outputPath = applicationHome.getDir().getParentFile()
                .getParentFile().getAbsolutePath() + "/src/main/resources/export/" +suffix+ ".xlsx";
        return FileUtil.createFile(outputPath);
    }
}
